﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace UIT_CV_Management
{
    public class DaoTaoDaiHoc
    {
        public String id;
        public String maDaoTaoDaiHoc;
        public String maChiTietNoiHoc;

        //database
        Database myDatabase = Database.shareDatabase;

        public ChiTietNoiHoc chiTietNoiHoc
        {
            get
            {
                return new ChiTietNoiHoc(maChiTietNoiHoc);
            }
            set
            {
                chiTietNoiHoc = value;
            }
        }

        public DaoTaoDaiHoc(ChiTietNoiHoc _chiTietNoiHoc)
        {
            // get id form database
            id = this.getIDFromDatabase();

            //properties
            maDaoTaoDaiHoc = this.getMaDaoTaoDaiHoc();
            maChiTietNoiHoc = _chiTietNoiHoc.maChiTietNoiHoc;

            //object 
            chiTietNoiHoc = _chiTietNoiHoc;
        }
        public DaoTaoDaiHoc(ChiTietNoiHoc _chiTietNoiHoc,int _index)
        {
            // get id form database
            id = this.getIDFromDatabase();
            this.IDaddIndex(_index);

            //properties
            maDaoTaoDaiHoc = this.getMaDaoTaoDaiHoc();
            maChiTietNoiHoc = _chiTietNoiHoc.maChiTietNoiHoc;

            chiTietNoiHoc = _chiTietNoiHoc;
        }
        public DaoTaoDaiHoc(String _id)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM DaoTaoDaiHoc WHERE ID = '" + _id + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                id = table.Rows[0]["ID"].ToString();
                maDaoTaoDaiHoc = table.Rows[0]["MaDaoTaoDaiHoc"].ToString();
                maChiTietNoiHoc = table.Rows[0]["MaNoiHoc"].ToString();
            }
        }
        private String getIDFromDatabase()
        {
            String x;
            x = Database.getID("DaoTaoDaiHoc", "ID");
            return x;
        }
        private String getMaDaoTaoDaiHoc()
        {
            String x;
            x = Database.getID("DaoTaoDaiHoc", "MaDaoTaoDaiHoc");
            return x;
        }

        private void IDaddIndex(int _index)
        {

        }
        public void save()
        {
            String sqlDaoTaoDaiHoc = "INSERT INTO DaoTaoDaiHoc VALUES('" + id + "','" + maDaoTaoDaiHoc + "','" + maChiTietNoiHoc + "')";
            QueryResult result = myDatabase.Query(sqlDaoTaoDaiHoc);
            if (result.ReturnCode == 409)
                MessageBox.Show("Lỗi khi lưu dữ liệu vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else // save successfully
            {
                chiTietNoiHoc.save();
            }
        }
        public void update()
        {

        }
        public void delete()
        {
            String sqlDelete = "DELETE FROM DaoTaoDaiHoc WHERE ID = '" + id + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                MessageBox.Show("Lỗi xoá dữ liệu trong database.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                chiTietNoiHoc.delete();
                
            }
        }
    }
}
